import re
import string

from common.exceptions import ParameterException


class ArgsValidate:

    @staticmethod
    def password_validate(value, name):
        pattern = f"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[{string.punctuation}])[A-Za-z\d{string.punctuation}]{{8,20}}$"
        result = re.findall(pattern, value)
        if not result:
            raise ValueError('用户密码至少包含一个大写字母、一个小写字母、一个数字和一个特殊字符且长度介于8~20个字符之间')
        return value

    @staticmethod
    def username_validate(value, name):
        if len(value) > 50 or len(value) < 1:
            raise ValueError('用户名长度应介于1~20个字符之间')
        return value

    @staticmethod
    def phone_validate(value, name):
        if len(value) > 20 or len(value) < 1:
            raise ValueError('用户名长度应介于1~20个字符之间')
        return value

    @staticmethod
    def email_validate(value, name):
        if len(value) > 20 or len(value) < 1:
            raise ValueError('用户名长度应介于1~20个字符之间')
        return value

    @staticmethod
    def code_validate(value, name):
        if len(value) != 6:
            raise ValueError('验证码错误')
        return value
